﻿using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.ModelBinding;

namespace Timo.Api.Core.Filter;

/// <summary>
/// 资源请求筛选器
/// </summary>
public class ResourceFilter : IAsyncResourceFilter
{
    public async Task OnResourceExecutionAsync(ResourceExecutingContext context, ResourceExecutionDelegate next)
    {
        // 获取所有资源提供器工厂
        var valueProviderFactories = context.ValueProviderFactories;

        // 比如这里判断如果是 Form 表单方式提交就就不给参数复制
        var formValueProviderFactory = valueProviderFactories
                .OfType<FormValueProviderFactory>()
                .FirstOrDefault();
        if (formValueProviderFactory != null)
        {
            // 移除 Form 表单绑定值提供器器
            context.ValueProviderFactories.Remove(formValueProviderFactory);
        }

        // .... 更多操作

        // 资源请求成功后
        var resourceContext = await next();
    }
}
